global chart_int_diff_options ///
	cells("mean(fmt(2)) sd p10 p50 p90") noobs fragment nonumber ///
	collabels(none) replace substitute(\hline "") nomtitles	

*******************************************************************************

*------------------------------------------------------------------------------*
* Interest rate differentials by LTV *
*------------------------------------------------------------------------------*

*--- Table
use "${datadir}\HTB_main_cleaned_counterfactual", clear

eststo clear
forvalues i = 1/3 {
	// group description
	if `i' == 1 {
		local title "LTV $\leq$ 75"
	}
	else if `i' == 2 {
		local title "75 $<$ LTV $\leq$ 85"
	}
	else {
		local title  "LTV $>$ 85"
	}
	
	estpost sum interest_diff if subsample & band_large == `i', detail
	esttab using "${tabledir}\price_diff_subsample`i'.tex", ///
		${chart_int_diff_options} coeflabel(interest_diff "\quad `title' ")
		
	estpost sum interest_diff if band_large == `i', detail
	esttab using "${tabledir}\price_diff_main`i'.tex",  ///
		${chart_int_diff_options} coeflabel(interest_diff "\quad `title' ") 
		
	estpost sum interest_diff if year >= 2015 & band_large == `i', detail
	esttab using "${tabledir}\price_diff_robust`i'.tex",  ///
		${chart_int_diff_options} coeflabel(interest_diff "\quad `title' ") 
		
	estpost sum interest_fee_diff if year >= 2015 & band_large == `i', detail
	esttab using "${tabledir}\price_diff_robust_fee`i'.tex", ///
		${chart_int_diff_options} ///
		coeflabel(interest_fee_diff "\quad `title' (match with fee band)")
}


	

*------------------------------------------------------------------------------*
* Payment decomposition *
*------------------------------------------------------------------------------*

use "${datadir}\HTB_main_cleaned_counterfactual_sub", replace

gen payment_round 					= round(payment, .1) * 1000
gen payment_counter_rate_round = round(payment_counter_rate, .1) * 1000
gen payment_counter_round 		= round(payment_counter, .1) * 1000

keep if band_large == 3			// LTV > 85

gen tmp = _N
 
bys payment_round: gen tmp1 				= _N
replace tmp1 								= 100 * tmp1/tmp
bys payment_round: gen fo_tmp1 				= (_n==1)

bys payment_counter_rate_round: gen tmp2	= _N
replace tmp2 								= 100 * tmp2/tmp
bys payment_counter_rate_round: gen fo_tmp2 = (_n==1)

bys payment_counter_round: gen tmp4 		= _N
replace tmp4 								= 100 * tmp4/tmp
bys payment_counter_round: gen fo_tmp4 		= (_n==1)

sum payment if payment_round < 2500, d
local med1  = round(`r(p50)'* 1000) 

sum payment_counter_rate if payment_counter_rate < 2500, d
local med2  = round(`r(p50)'* 1000)  

sum payment_counter if payment_counter < 2500, d
local med3  = round(`r(p50)'* 1000) 


twoway 		(connected tmp1 payment_round				if fo_tmp1==1 & payment_round < 2500, msize(medsmall) sort ) ///
			(connected tmp2 payment_counter_rate_round	if fo_tmp2==1 & payment_counter_rate_round < 2500, msize(medsmall) sort msymbol(X)) ///
			/// (connected tmp3 payment_counter_loan_round	if fo_tmp3==1 & payment_counter_loan_round < 50, msize(medsmall) sort msymbol(T)) ///
			(connected tmp4 payment_counter_round		if fo_tmp4==1 & payment_counter_round < 2500, msize(medsmall) sort msymbol(D)), /// 
			/// xline(0, lwidth(vthin) lcolor(black) lpattern(dash)) ///		
			text(15 1600  "Median true rate, true loan = £`med1'", size(med)) ///
			text(12 1600  "Median EL rate, true loan = £`med2'", size(med)) ///
			text(9 	1600  "Median EL rate, EL loan = £`med3'", size(med)) ///
			ytitle("Share of Mortgages (%)") ///
			xtitle("Monthly payment (£)") ylabel(0(4)16)   ///
			legend(label( 1 "True rate, true loan" ) label( 2 "EL rate, true loan") label(3 "EL rate, EL loan" ) r(2)) ///
			graphregion(color(white)) bgcolor(white) name(gr, replace)
drop *tmp*

graph export "${figuredir}\kdensity_paydecomp_PB.pdf", replace 
